<template>
  <a-modal
    title="操作"
    style="top: 20px;"
    :width="800"
    v-model="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
  >
    <a-form :form="form">
      <a-form-item style="display:none">
        <a-input v-decorator="['custId']"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="客户名称">
        <a-input placeholder="客户名称" v-decorator="['custName',{rules: [{required: true, message: '请输入客户名称'}]}]"/>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="所在行业">
        <a-select  style="width: 100%"  v-decorator="['indId', {rules: [{ required: true, message: '请选择所在行业' }]}]" :allowClear="true">
          <a-select-option v-for="(d,index) in project_industry_typeDictList" :key="index" :value="d.dictValue" >{{ d.dictLabel }}</a-select-option>
        </a-select>
      </a-form-item>
      <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="行业细分">
        <a-select  style="width: 100%"  v-decorator="['indClass', {rules: [{ required: true, message: '请选择行业细分' }]}]" :allowClear="true">
          <a-select-option v-for="(d,index) in indClass_TableDict" :key="index" :value="d.key" >{{ d.value }}</a-select-option>
        </a-select>
      </a-form-item>
    </a-form>
  </a-modal>
</template>
<script>
import moment from "moment";
import { dictList,getDictMapKey,tableDictList,getTableDictKey } from '@/utils/dict'
import { saveCustom,getCustomInfo } from '@/api/project/custom'
import pick from 'lodash.pick'

export default {
  name: 'CustomModal',
  props: {
  },
  components: {
  },
  data () {
    return {
      visible: false,
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      indClass:[],
      mdl: {},
      form: this.$form.createForm(this)
    }
  },
  beforeCreate () {
  },
  created () {
    this.dictSync();
  },
  methods: {
    async dictSync(){
      this.project_industry_typeDictList=await dictList('project_industry_type');
      this.indClass_TableDict=await tableDictList('indClass');
    } ,
    add () {
      this.form.resetFields()
      this.edit()
    },
    edit (custId) {
      if(custId){
        getCustomInfo(custId).then(res => {
          this.mdl = Object.assign(res)
          this.visible = true
          ;
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(this.mdl,'custId','custName','indId','indClass'))
          })
        })
      }else{
        this.visible = true
        this.mdl={}
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.mdl,'custName','indId','indClass'))
        })
      }
    },
    handleSubmit (e) {
      e.preventDefault()
      this.form.validateFields((err, values) => {
        if (!err) {
          console.log('Received values of form: ', values)
          this.confirmLoading = true
          saveCustom(values).then(res => {
            if (res.code === 0) {
              this.$message.success('保存成功')
              this.$emit('ok')
              this.visible = false
            } else {
              this.$message.success(res.msg)
            }
          }).catch(() => {
            this.$message.error('系统错误，请稍后再试')
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    }
  },
  watch: {
    /*
      'selectedRows': function (selectedRows) {
        this.needTotalList = this.needTotalList.map(item => {
          return {
            ...item,
            total: selectedRows.reduce( (sum, val) => {
              return sum + val[item.dataIndex]
            }, 0)
          }
        })
      }
      */
  }
}
</script>
